/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ActiveRecord.Newdb;

import java.sql.Connection;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author degrave
 */
public class Db {
    private static Db self=null;
    
    String url = "jdbc:derby://localhost:1527/vc_db;create=true";
    //String databaseURL = "jdbc:firebirdsql:local:d:/DB/is.fdb?sql_dialect=1";
    String user = "root";
    String password = "toor";   
    String driverName = "org.apache.derby.jdbc.ClientDriver";
    
    java.sql.Driver d = null;
    java.sql.Connection c = null;
    java.sql.Statement s = null;
    java.sql.ResultSet rs = null;
    
    private Db(){
        
         // Let's try to register the driver with the driver manager
        try {
            Class.forName (driverName);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Db.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        // At this point the driver should be registered with the driver manager.
        // Try to find the registered driver that recognizes interbase URLs...
        try {
            // We pass the entire database URL, but we could just pass "jdbc:interbase:"
            d = java.sql.DriverManager.getDriver (url);
            System.out.println ("driver version " +
                            d.getMajorVersion () +
                            "." +
                            d.getMinorVersion () +
                            " registered with driver manager.");
        }
        catch (java.sql.SQLException e) {
            System.out.println ("Unable to find driver among the registered drivers.");
            showSQLException (e);
            return;
        }
            
        // If you're working with a particular driver d, which may or may not be registered,
        // you can get a connection directly from it, bypassing the driver manager...
        try {
          java.util.Properties connectionProperties = new java.util.Properties ();
          connectionProperties.put ("user", user);
          connectionProperties.put ("password", password);
          connectionProperties.put ("lc_ctype", "WIN1251");
          c = d.connect (url, connectionProperties);
          System.out.println ("Connection complite.");
          s = c.createStatement();
          //s.executeUpdate(createTableUsers);
          //s.executeUpdate(createTableCalendars);
          //s.executeUpdate(createTableEvents);          
        }
        catch (java.sql.SQLException e) {
          System.out.println ("Unable to establish a connection through the driver.");
          showSQLException (e);
        }
        
   }  
    public static Db getInstance(){
        if(self==null){
            self = new Db();
        }
        return self;
    }  
    public Connection getConnection() {
        return c;
    }
    
    // Display an SQLException which has occured in this application.
    private static void showSQLException (java.sql.SQLException e)
    {
    // Notice that a SQLException is actually a chain of SQLExceptions,
    // let's not forget to print all of them...
    java.sql.SQLException next = e;
    while (next != null) {
        System.out.println (next.getMessage ());
        System.out.println ("Error Code: " + next.getErrorCode ());
        System.out.println ("SQL State: " + next.getSQLState ());
        next = next.getNextException ();
    }
    }
}   